﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Crosscutting.Authorize
{
    public class RoleService : IRoleService
    {
        Domain.Uow.IRepository<Domain.Entity.Role> roleRespoitory;

        public RoleService(Domain.Uow.IRepository<Domain.Entity.Role> role)
        {
            if (role == null)
            {
                throw new ArgumentNullException("roleRespoitory is null");
            }
            roleRespoitory = role;
        }

        public IQueryable<Domain.Entity.Role> GetList()
        {
            var res = roleRespoitory.GetList().AsQueryable();
            return res;
        }


        public Domain.Entity.Role GetEmptyRole()
        {
            var role = new Domain.Entity.Role();
            role.AddTime = System.DateTime.Now;
            role.Id = 0;
            role.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
            role.Name = "空角色";
            role.Remark = "空角色";
            role.SortNum = 0;
            role.State = true;
            return role;
        }

        public Domain.Entity.Role GetDefaultRole()
        {
            var role = new Domain.Entity.Role();
            role.AddTime = System.DateTime.Now;
            role.Id = 0;
            role.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
            role.Name = "普通用户";
            role.Remark = "普通用户";
            role.SortNum = 0;
            role.State = true;
            return role;
        }

        public Domain.Entity.Role Add(Domain.Entity.Role role, Domain.Entity.User opeUser)
        {
            roleRespoitory.Add(role);
            roleRespoitory.Uow.Commit();
            return role;
        }

        public bool Delete(Domain.Entity.Role role, Domain.Entity.User opeUser)
        {
            var res = false;
            try
            {
                role.State = false;
                roleRespoitory.Modify(role);
                roleRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(Domain.Entity.Role role, Domain.Entity.User opeUser)
        {
            var res = false;
            try
            {
                roleRespoitory.Delete(role);
                roleRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Delete(IList<int> idList, Domain.Entity.User opeUser)
        {
            var res = false;
            var deleteList = roleRespoitory.GetList(e => idList.Contains(e.Id));
            foreach (var item in deleteList)
            {
                item.State = false;
                roleRespoitory.Modify(item);
            }
            try
            {
                roleRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User opeUser)
        {
            var res = false;
            var delete = roleRespoitory.GetList(e => idList.Contains(e.Id));
            foreach (var item in delete)
            {
                roleRespoitory.Delete(item);
            }
            try
            {
                roleRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool Modify(Domain.Entity.Role role, Domain.Entity.User opeUser)
        {
            var res = false;
            roleRespoitory.Modify(role);
            try
            {
                roleRespoitory.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public Domain.Entity.Role Get(int id)
        {
            var data = roleRespoitory.Get(id);
            return data;
        }
    }
}
